home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1998 August: Tool Chest / Dev.CD Aug 98 TC.toast / Tool Chest / Testing & Debugging / Virtual User / Virtual User Current Release / Examples / Example Libraries / Time.vulib < prev    next >
Encoding:
Text File  |  1998-06-04  |  4.5 KB  |  131 lines  |  [TEXT/MPS ]

  1. #
  2. #    File:        TimeLib.vu
  3. #
  4. #    Contains:    Tasks that make it easier to work with time in VU.
  5. #
  6. #    Written by:    Sean Flynn
  7. #
  8. #    Copyright:    © 1990-1992 by Apple Computer, Inc., all rights reserved.
  9. #
  10. #    Change History (most recent first):
  11. #
  12. #        <4>         6/17/92    DGG            Changed format of printTime, added getTime
  13. #        <3>         8/26/91    Rick        Marked new Tasks
  14. #                 6/11/91    Rick        fixed task TimeElapsed
  15. #                 1/17/91    Rick        added task TimeElapsed
  16. #                 1/25/91    naga        modified header and edited formats 
  17. #
  18. #    To Do:
  19. #
  20.  
  21. (************************************************************************************
  22. *Task WaitForTime( endHour := 0, endMinute := 0, endSecond := 0 )
  23. *    WaitForTime task.  Wait until a specified time of day.
  24. ************************************************************************************)
  25. task WaitForTime( endHour := 0, endMinute := 0, endSecond := 0 )
  26. begin
  27.     while match [time h:?h2 s:?s2] and
  28.             ( h2 / 100 <> endHour  or
  29.               h2 mod 100 < endMinute or
  30.               s2 < endSecond ) do;
  31. end;
  32.  
  33. (************************************************************************************
  34. * Task GetCurrentTime(returnDate := true)
  35. *    This task returns the current time.  The format is as follows:
  36. *            <hour of day>:<minutes>:<seconds> <AM or PM>
  37. *    It also takes one parameter that indicates whether the date should be printed.
  38. *    If the actual parameter passed is a true value, the current date is also printed.
  39. *    If no parameter is passed, the date is printed by default.
  40. *    The format of the date is as follows:
  41. *            <month>/<day>/<year>
  42. *    
  43. ************************************************************************************)
  44. task GetCurrentTime(returnDate := true)
  45. begin
  46.     timeString := "";
  47.     match [time h:?hour s:?secs d:?day m:?month y:?year];
  48.     hourOfDay := (hour / 100);
  49.     if (hour < 1200) 
  50.     begin # in AM
  51.         if (hourOfDay) 
  52.             timeString := numToStr(hourOfDay);
  53.         else 
  54.             timeString := '12';
  55.         meridian := ' AM';
  56.     end;
  57.     else 
  58.     begin # in PM
  59.         if (hourOfDay = 12)
  60.             timeString := timeString + numToStr(hourOfDay);
  61.         else
  62.             timeString := timeString + numToStr(hourOfDay - 12);
  63.         meridian := ' PM';
  64.     end;
  65.     timeString := timeString + ':' + numToStr((hour mod 100) / 10) + 
  66.                     numToStr(hour mod 10) + ':' + numToStr(secs) + meridian;
  67.     if (returnDate) 
  68.         timeString := timeString + " " + numToStr(month) + "/" + numToStr(day) + "/" + 
  69.                         numToStr((year mod 100) / 10) + numToStr(year mod 10);
  70.     return timeString;
  71. end;
  72.  
  73. (************************************************************************************
  74. *Task PrintCurrentTime(returnDate) 
  75. *    This task pretty prints the current time.  The format is as follows:
  76. *            <hour of day>:<minutes>:<seconds> <AM or PM>
  77. *    It also takes one parameter that indicates whether the date should be printed.
  78. *    If the actual parameter passed is a true value, the current date is also printed.
  79. *    If no parameter is passed, the date is printed by default.
  80. *    The format of the date is as follows:
  81. *            <month>/<day>/<year>
  82. *    
  83. ************************************************************************************)
  84. task PrintCurrentTime(printDate := true) 
  85. begin
  86.     println GetCurrentTime(printDate);
  87. end;
  88.  
  89. (************************************************************************************
  90. * Task TimeElapsed( startTime, stopTime )
  91. *    This task computes the difference between the start & stop time descriptors and 
  92. *    returns the result as a time desciptor. The dates are ignored.
  93. ************************************************************************************)
  94. task TimeElapsed( StartTime, stopTime )
  95. begin
  96.  
  97.     startHour    := startTime.h / 100;
  98.     startMinute    := startTime.h mod 100;
  99.     startSecond    := startTime.s;
  100.     
  101.     stopHour    := stopTime.h / 100;
  102.     stopMinute    := stopTime.h mod 100;
  103.     stopSecond    := stopTime.s;
  104.  
  105.             #####    Subtract seconds column
  106.     if( stopSecond < startSecond )             ### must borrow from minutes column
  107.     begin
  108.         if( stopMinute = 0 )                ### must borrow from hours column
  109.         begin
  110.             stopHour := stopHour - 1;        ### borrow 60 minutes from hours column
  111.             stopMinute := stopMinute + 60;
  112.         end;
  113.         stopMinute := stopMinute - 1;        ### borrow 60 seconds from minutes column
  114.         stopSecond := stopSecond + 60;
  115.     end;
  116.     stopSecond := stopSecond - startSecond;    ### finally subtract the seconds
  117.  
  118.             #####    Subtract minutes column
  119.     if( stopMinute < startMinute )            ### must borrow from hours column
  120.     begin
  121.         stopHour := stopHour - 1;            ### borrow 60 minutes from hours column
  122.         stopMinute := stopMinute + 60;
  123.     end;
  124.     stopMinute := stopMinute - startMinute;    ### finally subtract the minutes
  125.  
  126.             #####    Subtract Hours column
  127.     stopHour := stopHour - startHour;
  128.         
  129.     return [ time y:0 m:0 d:0 h:(( stopHour * 100 ) + stopMinute) s:stopSecond ];
  130. end;
  131.